#include"init.h"
#include"debug.h"
#include"thread.h"
#include"interrupt.h"
#include"console.h"
#include"process.h"
#include"syscall.h"
#include"stdio.h"
#include"stdio_kernal.h"
#include"ide.h"
#include"timer.h"
#include"file.h"
#include"syscall.h"
#include"yi_shell.h"
#include"elf32.h"

//init process
void init(void){
	pid_t child_id;	
	int32_t exit_status;
	child_id=fork();
	if(child_id<0){
		printf("fork failed\n");
	}else if(!child_id){
		yi_shell();
	}
	while(1){
		wait(&exit_status);
	}
}


static void ext_command_setup(void){
	int32_t fd;
	int32_t w_cnt;
	uint8_t s_cnt;
	uint32_t filesize=10660;
	char* buffer;
	printk("try load hello \n");
	struct disk* d1=nm2disk(0,0);
	ASSERT(d1!=NULL);
	s_cnt=div_round_up(filesize,BLOCK_SIZE);
	buffer=(char*)sys_malloc(BLOCK_SIZE*s_cnt);
	ASSERT(buffer!=NULL);
	ide_read(d1,buffer,300,s_cnt);
	if((fd=sys_open("/hello",O_CREAT|O_RDWR))<0){
		printk("create hello failed\n");
		return;
	}

	if((w_cnt=sys_write(fd,buffer,filesize))<0){
		printk("write hello failed\n");
		return;
	}
	sys_close(fd);
}

int main(void){
	init_kernal();
	intr_enable();
	ext_command_setup();
	process_execute((void*)init,"init");
	while(1);
	return 0;
}
